package com.zplus.managers.petservices.controllers;
import com.zplus.managers.petservices.entities.AdoptRequest;
import com.zplus.managers.petservices.services.AdoptRequestSerivces;
import com.zplus.managers.petservices.services.RequestState;
import com.zplus.managers.petservices.utils.Message;
import com.zplus.managers.petservices.utils.Page;
import com.zplus.managers.petservices.utils.PagingFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/adoptrequest")
public class AdoptRequestController
{
    @Autowired
    private AdoptRequestSerivces adreqService;
    private PagingFactory pagingFactory;

    /**
     * get all requests generated at given date
     * @param date given date, a string matching format "YYYY-MM-DD"
     * @return message with first page contains request data
     */
    @RequestMapping(value = "/getbydate", method = RequestMethod.GET)
    Message getRequsetAtDate(@RequestParam("queryDate") String date)
    {
        List<AdoptRequest> adoptRequests = adreqService.getRequsetByDate(date);
        Message message = new Message();
        message.load(adoptRequests);
        return message;
    }
    /**
     * get all request of given type
     * @param pageSize num of items that a page can hold
     * @param type type of request, accept 'pass', 'reject', 'uncheck';
     * @return Message contains first page
     */
    @RequestMapping(value = "/getallreq", method = RequestMethod.GET)
    Message getAllRequest(@RequestParam("pageSize")int pageSize,@RequestParam("type") String type)
    {
        List<AdoptRequest> lst = adreqService.getAllRequest(type);
        Message msg = new Message();
        if(lst != null)
        {
            pagingFactory = new PagingFactory(lst, pageSize);
            msg.loadPage(pagingFactory.nextPage());
            msg.ok();
        }
        else
            msg.fail();

        return msg;
    }

    @RequestMapping(value = "/getpage", method = RequestMethod.GET)
    Message getPage(@RequestParam("index")int index)
    {
        Message msg = new Message();
        // check index & paging system validation
        if(pagingFactory == null || index <= 0 || (pagingFactory != null && index > pagingFactory.getTotalPages()))
        {
            msg.fail();
            return msg;
        }
        // get target page
        Page page = pagingFactory.getPage(index);
        msg.loadPage(page);
        return msg;
    }

    /**
     * return all request upload at given date
     * @param date select a date
     * @param pageSize num of items that a page can hold
     * @return Message contains first page
     */
    @RequestMapping(value = "/searchrequest", method = RequestMethod.GET)
    Message searchAdoptRequest(@RequestParam("date")String date,@RequestParam("pageSize") int pageSize)
    {

        Message msg = new Message();
        Page page = null;
        List<AdoptRequest> requsetByDate = adreqService.getRequsetByDate(date);
        if(requsetByDate.isEmpty())
        {
            msg.fail("empty result");
            return msg;
        }
        pagingFactory = new PagingFactory(requsetByDate, pageSize);
        page = pagingFactory.nextPage();
        msg.loadPage(page);
        msg.ok();

        return msg;
    }



    @RequestMapping(value = "/acceptrequest", method = RequestMethod.GET)
    Message acceptRequest(@RequestParam("reqid") String reqid,@RequestParam("isPass") String isPass)
    {
        int rqid = Integer.parseInt(reqid);
        boolean res = false;
        // get reqeust by id
        AdoptRequest req = adreqService.getReqeustById(rqid);
        if (isPass.equals("true")|| isPass.equals("True"))
            // accept
            res = adreqService.updateRequestState(req, RequestState.YES);
        else
            //reject
            res = adreqService.updateRequestState(req, RequestState.NO);

        Message msg = new Message();
        if(res)
            msg.ok();
        else
            msg.fail();
        return msg;
    }

    @RequestMapping(value = "/regrequest",method = RequestMethod.POST)
    Message regRequest(@RequestBody AdoptRequest newReq)
    {
        Message msg = new Message();
        if( adreqService.newAdoptReqest(newReq))
            msg.ok();
        else
            msg.fail();
        return msg;
    }
}
